/**============================================================================
 Confidential Information - Limited distribution to authorized persons only.
 This software is protected as an unpublished work under the U.S. Copyright
 Act of 1976.

 Copyright (c) 2011, BluePhoenix Solutions, Inc. All rights reserved.
 ----------------------------------------------------------------------------
 file......: PredCoreStringUtils.java
 Author....: Vyacheslav Kovalyov
 Created...: 28.10.11

 Description:

 ============================================================================*/
package ru.monyag.pred.core.utils;

import java.util.ArrayList;
import java.util.List;

import static org.apache.commons.lang.StringUtils.*;
import static org.springframework.util.CollectionUtils.isEmpty;
import static ru.monyag.pred.core.utils.PredCoreConstants.SPACE;
import static ru.monyag.pred.core.utils.PredCoreConstants.TAGS_SEPARATOR;

/**
 * @author Vyacheslav Kovalyov
 * @version 1.0
 */
public abstract class PredCoreStringUtils {

    public static List<String> parseTags(String tagsString) {

        List<String> tagsList = new ArrayList<String>();
        if (isNotBlank(tagsString)) {

            String[] tags = tagsString.split(TAGS_SEPARATOR);

            for (String s : tags) {
                String tag = trimToEmpty(s);

                if (isNotBlank(s) && !tagsList.contains(s)) {
                    tagsList.add(tag);
                }
            }
        }

        return tagsList;
    }

    public static String parseTags(List<String> tagsList) {

        if (isEmpty(tagsList)) {
            return EMPTY;
        }

        StringBuilder builder = new StringBuilder();

        for (String s : tagsList) {
            if (isNotBlank(s)) {
                builder.append(s);
                if (tagsList.get(tagsList.size() - 1) != s) {
                    builder.append(TAGS_SEPARATOR);
                    builder.append(SPACE);
                }
            }
        }

        return builder.toString();
    }
}
